//
//  DefineConstant.h
//  ZhangShangDaXue
//
//  Created by Arthur Wang on 14-3-11.
//  Copyright 2014 cloud-loong. All rights reserved.
//
//
#import <Foundation/Foundation.h>
#ifndef HaiTaoAn_DefineConstant_h
#define HaiTaoAn_DefineConstant_h   1


///////////////**本地化转换*/
#pragma mark - Local Exchange functions
#pragma mark   本地转化

#define L(key) \
[[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]


///////////////**坐标相关*/
#pragma mark - Coordinate Functions
#pragma mark   坐标相关

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define STATUSBAR_HEIGHT [[UIApplication sharedApplication] statusBarFrame].size.height

///////////////*页面大小相关*/
#pragma mark - font functions
#pragma mark   页面大小
#define kPROPORTION  ([[UIScreen mainScreen]bounds].size.width/320)
#define swipProportion  0.8
#define BOUNDWIDTH     [[UIScreen mainScreen] bounds].size.width
#define BOUNDHEIGHT    [[UIScreen mainScreen] bounds].size.height
#define VIEWWIDTH      self.view.frame.size.width
#define VIEWHEIGHT     self.view.frame.size.height
#define OriginX(view)      (view.frame.origin.x+view.frame.size.width)
#define OriginY(view)      (view.frame.origin.y+view.frame.size.height)
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
//#define DefaultColor [UIColor colorWithRed:(22.0/255) green:(114.0/255) blue:(251.0/255) alpha:1]

#define DefaultColor [UIColor colorWithRed:(2.0/255) green:(109.0/255) blue:(255.0/255) alpha:1]

#define ShuoShuoColor [UIColor colorWithRed:(41.0/255) green:(89.0/255) blue:(152.0/255) alpha:1]

#define shuoshuoFont   [UIFont systemFontOfSize:14]

#define kPushViewTag        909090

#define kScreenSize  [[UIScreen mainScreen] bounds].size
#define kViewSize    self.view.frame.size
#define kViewOrigin  self.view.frame.origin

//#define kPROPORTION   1 //以肾6为基准，折算一个分辨率的比例

//登陆相关信息
#define kLoginStatusMessageStartHttp				@"正在发送登录请求"
#define kLoginStatusMessageRequireUserName			@"请输入用户名"
#define kLoginStatusMessageRequirePassword			@"请输入密码"
#define kIsLogin    @"isLogin"


///////////////*颜色相关*/
#pragma mark - color functions
#pragma mark   颜色
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#define Font(n) [UIFont systemFontOfSize:n]
#define BoldFont(n) [UIFont boldSystemFontOfSize:n]

#define HSVCOLOR(h,s,v) [UIColor colorWithHue:h saturation:s value:v alpha:1]
#define HSVACOLOR(h,s,v,a) [UIColor colorWithHue:h saturation:s value:v alpha:a]

/// rgb颜色转换（16进制->10进制）
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]


///////////////*角度弧度转化*/
#pragma mark - degrees/radian functions
#pragma mark   角度弧度转化
#define degreesToRadian(x) (M_PI * (x) / 180.0)
#define radianToDegrees(radian) (radian*180.0)/(M_PI)


///////////////*block相关*/
#pragma mark - Block functions
#pragma mark   block相关
///block 声明
#ifdef NS_BLOCKS_AVAILABLE
typedef void (^SNBasicBlock)(void);
typedef void (^SNOperationCallBackBlock)(BOOL isSuccess, NSString *errorMsg);
typedef void (^SNCallBackBlockWithResult)(BOOL isSuccess, NSString *errorMsg,id result);
typedef void (^SNArrayBlock)(NSArray *list);
#endif

//防止block循环引用
#define WS(weakSelf)  __weak __typeof(self)weakSelf = self;
#define SS(strongSelf) __strong typeof(self)strongSelf = self;


///////////////*GCD线程相关*/
#pragma mark - Thread functions
#pragma mark   GCD线程相关
///线程执行方法 GCD
#define PERFORMSEL_BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
#define PERFORMSEL_SYNC_BACK(block) dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
#define PERFORMSEL_MAIN(block) dispatch_async(dispatch_get_main_queue(),block)


///////////////*为空判断相关*/
#pragma mark - Empty judge functions
#pragma mark   为空判断相关
///是否为空或是[NSNull null]
#define NotNilAndNull(_ref)  (((_ref) != nil) && (![(_ref) isEqual:[NSNull null]]))
#define IsNilOrNull(_ref)   (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]))
///字符串是否为空
#define IsStrEmpty(_ref)    (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]) ||([(_ref)isEqualToString:@""]))
///数组是否为空
#define IsArrEmpty(_ref)    (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]) ||([(_ref) count] == 0))


///////////////*SNAssert 断言*/
#define SNAssert(expression, ...) \
do { if(!(expression)) { \
DLog(@"%@", [NSString stringWithFormat: @"Assertion failure: %s in %s on line %s:%d. %@", #expression, __PRETTY_FUNCTION__, __FILE__, __LINE__, [NSString stringWithFormat:@"" __VA_ARGS__]]); \
abort(); }} while(0)


///////////////*类型数据生成相关*/
#pragma mark -  DataTypes Generation functions
#pragma mark    类型数据生成相关
///便捷方式创建NSNumber类型
#undef	__INT
#define __INT( __x )			[NSNumber numberWithInt:(NSInteger)__x]

#undef	__UINT
#define __UINT( __x )			[NSNumber numberWithUnsignedInt:(NSUInteger)__x]

#undef	__FLOAT
#define	__FLOAT( __x )			[NSNumber numberWithFloat:(float)__x]

#undef	__DOUBLE
#define	__DOUBLE( __x )			[NSNumber numberWithDouble:(double)__x]

#undef	__STRINGNOTNIL
#define	__STRINGNOTNIL( __x )   (__x?__x:@"")

#define OC(str) [NSString stringWithCString:(str) encoding:NSUTF8StringEncoding]



/**
 * # 将宏的参数字符串化，C 函数 strchr 返回字符串中第一个 '.' 字符的位置
 */
#define Keypath(keypath) (strchr(#keypath, '.') + 1)


///////////////*系统版本判断*/
#pragma mark -  SystemInfo functions
#pragma mark

///系统版本判断
#define IOS7_OR_LATER   ( [[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending )
#define IOS6_OR_LATER	( [[[UIDevice currentDevice] systemVersion] compare:@"6.0"] != NSOrderedAscending )
#define IOS5_OR_LATER	( [[[UIDevice currentDevice] systemVersion] compare:@"5.0"] != NSOrderedAscending )
#define IOS4_OR_LATER	( [[[UIDevice currentDevice] systemVersion] compare:@"4.0"] != NSOrderedAscending )
#define IOS3_OR_LATER	( [[[UIDevice currentDevice] systemVersion] compare:@"3.0"] != NSOrderedAscending )

#define isIPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define isIPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
#define isIPhone6p ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)

#define IOS7 ([[[UIDevice currentDevice]systemVersion] floatValue] >= 7.0)
#define IOS8 ([[[UIDevice currentDevice]systemVersion] floatValue] >= 8.0)


///////////////*页面元素相关*/
#pragma mark -  View Element functions
#pragma mark    页面元素相关
///弹出提示框
#define BBAlertMessage(__MSG) \
{\
    BBAlertView *alert = [[BBAlertView alloc] initWithStyle:BBAlertViewStyleDefault\
    Title:L(@"system-info")\
    message:(__MSG)\
    customView:nil\
    delegate:nil\
    cancelButtonTitle:L(@"Confirm")\
    otherButtonTitles:nil];\
    [alert show];\
}

#define TT_RELEASE_SAFELY(__REF) 


///view安全释放
#define TTVIEW_RELEASE_SAFELY(__REF) \
{\
    if (nil != (__REF))\
    {\
        [__REF removeFromSuperview];\
        __REF = nil;\
    }\
}

///释放ASIHttpRequest专用
#define HTTP_RELEASE_SAFELY(__POINTER) \
{\
    if (nil != (__POINTER))\
    {\
        [__POINTER clearDelegatesAndCancel];\
    }\
}


///释放service专用
#define SERVICE_RELEASE_SAFELY(__REF) \
{\
    if ((__REF) != nil)\
    { \
        [__REF setDelegate:nil];\
    }\
}

///释放SNPopoverViewController
#define POP_RELEASE_SAFELY(__POINTER) \
{\
    if (nil != (__POINTER))\
    {\
        [__POINTER dismissPopoverAnimated:YES];\
    }\
}

//释放定时器
#define TT_INVALIDATE_TIMER(__TIMER) \
{\
    [__TIMER invalidate];\
    __TIMER = nil;\
}


#define SETUSERDEAFAULTS(object,key) NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];[userDef setObject:object forKey:key];

#define GETUSERDEAFAULTS(key) NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];[userDef objectForKey:key];


// 字典转化为字符串
extern NSString *EncodeObjectFromDic(NSDictionary *dic,NSString *key);

// 根据index从数组取值
extern id        safeObjectAtIndex(NSArray *arr, NSInteger index);

#define S000    @"S000"         //成功
#define E000    @"E000"         //无数据
#define E001    @"E001"         //未登陆
#define E002    @"E002"         //接口编号为空
#define E003    @"E003"         //接口编号错误
#define E004    @"E004"         //系统错误
#define E005    @"E005"         //入参错误
#define E006    @"E006"         //错误


//nsuserdefault的key值宏
//是否第一次登陆此程序
#define kEverLaunched      @"everLaunched"
#define kFirstLaunch       @"firstLaunch"

#define kAppVersion        @"appVersion"
#define kDownLoadZip       @"downLoad"

//网络请求是否需要更新软件是的入参
#define kSystemType        @"systemType"
#define kLoginInfo         @"loginInfo"
#define kSchoolInfo        @"schoolInfo"
#define kPersonalInfo      @"personalInfo"
#define kNewSchoolChat     @"newSchoolChat"


#endif
